home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '88 / Proceedings '88 / Feldt Advanced Mac Programming / Serial Port / lib src / XTCommMisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-26  |  4.6 KB  |  198 lines  |  [TEXT/KAHL]

  1. /*                        Commlib - XTCommMisc                                */
  2. /*                          Aztec C compiler 1.06i                            */
  3. /*                        Lightspeed C compiler 2.01                        */
  4.  
  5. /*        Copyright © 1985,86,87 by small systems guild.  All rights reserved.    */
  6.  
  7. #include  <extender.h>   /* include Commlib and standard Toolbox headers    */
  8.  
  9. Boolean ValidPointer(P)
  10. Ptr        P;
  11. {
  12.     if ((P == NULL) ||                        /* pointer is null (zero)    */
  13.             ((long)P & 1L) ||                /* pointer is odd            */
  14.             ((long)P > (long)TopMem()))        /* points above top of RAM    */
  15.         return(FALSE);                        /* then pointer is definitely bad!    */
  16.     else
  17.         return(TRUE);                        /* else pointer is potentially valid*/
  18.  
  19. Boolean ValidHandle(H)
  20. Handle    H;
  21. {
  22.     Ptr        P;
  23.  
  24.     if (ValidPointer((Ptr)H)) {                    /* the handle dereferenced is OK    */
  25.         P = (Ptr)((long)(*H) & 0x00FFFFFF);    /* mask off the master pointer status bits */
  26.         if (ValidPointer(P))                /* the pointer pointed to is OK    */
  27.             return(TRUE);                    /* then Handle is potentially valid    */
  28.     }
  29.     return(FALSE);                            /* Handle doesn't point to a pointer */
  30. }
  31.  
  32. void CommVersion(verStr)        /* copies version string to indicated location  */
  33. char    *verStr;
  34. {
  35.     XTpstrcpy(verStr,"\ssg Commlib -- version 3d10 Oct  1987");    
  36. }
  37.  
  38. OSErr XTCloseFile(reply)
  39. SFReply        *reply;
  40. {
  41.     int        fRefNum,errCode;
  42.  
  43.     if (!ValidPointer((Ptr)reply))
  44.         return(nilHandleErr);
  45.  
  46.     errCode = FSOpen(reply->fName,reply->vRefNum,&fRefNum);
  47.     if ((errCode == opWrErr) || (errCode == noErr)) {
  48.         FSClose(fRefNum);
  49.         return(noErr);
  50.     }
  51.     else
  52.         return(errCode);
  53. }
  54.  
  55. void BugAlert(s0,s1,s2,s3)
  56. char        *s0,*s1,*s2,*s3;
  57. {
  58.     EventRecord        event;
  59.     WindowRecord    wRec;
  60.     WindowPtr        wPtr;
  61.     Rect            tempRect;
  62.     char            *strPtr[4];
  63.     int                bytes,strLen,i;
  64.     char            theStr[256];
  65.                 
  66.     strPtr[0] = s0;
  67.     strPtr[1] = s1;
  68.     strPtr[2] = s2;
  69.     strPtr[3] = s3;
  70.     bytes = 0;
  71.  
  72.     for (i = 0; i < 4; i++) {            /* loop thru each of four strings    */
  73.         strLen = (unsigned char)strPtr[i];        /* get the string length    */
  74.         if (bytes + strLen > 255) {            /* if the strings are to long    */
  75.             strPtr[i][0] = (unsigned char)(255 - bytes); /* shorten    it temp    */
  76.             bytes = 255;                /* set bytes to 255 (max strlength)    */
  77.         }
  78.         bytes += (unsigned char)strPtr[i][0];        /* increment byte count    */
  79.         XTpstrconcat(theStr,strPtr[i]);        /* concatenate the two strings    */
  80.         strPtr[i][0] = (unsigned char)strLen;    /* restore original length    */
  81.     }
  82.  
  83.         SetRect(&tempRect,100,100,390,300);
  84.         wPtr = CreateWindow(&wRec,&tempRect,"\PBug Alert",1,TRUE,FALSE,FALSE,FALSE,FALSE);
  85.         TextFont(0);                    /* set grafport font to system font    */
  86.  
  87.         SetRect(&tempRect,20,20,270,140);
  88.         TextBox(&(theStr[1]),(unsigned char)theStr[0],&tempRect,teJustLeft);
  89.  
  90.         TextFont(1);                    /* set grafport font to application font    */
  91.         TextSize(10);
  92.         SetRect(&tempRect,10,174,280,190);
  93.         XTpstrcpy(theStr,"\PPress mouse button, return or enter to continue...");
  94.         TextBox(&(theStr[1]),(unsigned char)theStr[0],&tempRect,teJustCenter);
  95.         FrameRoundRect(&tempRect,16,16);
  96.  
  97.     do {
  98.         SystemTask();
  99.         GetNextEvent(mDownMask | keyDownMask,&event);
  100.         switch (event.what) {
  101.             case mouseDown:                /* mouse button pressed    */
  102.                 KillWindow(wPtr);
  103.                 RETURN;
  104.                 break;
  105.             case keyDown:
  106.                 switch ((int)(event.message & charCodeMask)) {
  107.                     case 3:                /* enter key pressed    */
  108.                     case 10:            /* return key pressed    */
  109.                         KillWindow(wPtr);
  110.                         RETURN;
  111.                         break;
  112.                 }
  113.                 break;
  114.         }
  115.     } while (TRUE);
  116. }
  117.  
  118. Boolean XTpstrcpy(toStr,fromStr)
  119. char    *toStr,*fromStr;
  120. {
  121.     int     i;
  122.  
  123.     if ((toStr == NULL) || (fromStr == NULL))
  124.         return(FALSE);
  125.  
  126.     for (i=0; i <= (unsigned char)(fromStr[0]); i++)
  127.         toStr[i] = fromStr[i];
  128.  
  129.     return(TRUE);
  130. }
  131.  
  132. Boolean XTcstrcpy(toStr,fromStr)        /* from K & R famous C book, p. 100 */
  133. char    *toStr,*fromStr;
  134. {
  135.     if ((toStr == NULL) || (fromStr == NULL))
  136.         return(FALSE);
  137.  
  138.     while (*toStr++ = *fromStr++)
  139.         ;
  140.  
  141.     return(TRUE);
  142. }
  143.  
  144. Boolean XTctopstr(cStr,pStr)
  145. char    *cStr,*pStr;
  146. {
  147.     int     i;
  148.  
  149.     if (!ValidPointer(cStr) || !ValidPointer(pStr))
  150.         return(FALSE);
  151.  
  152.     for (i = 0;cStr[i] != '\0';i++) {
  153.         pStr[i+1] = cStr[i];
  154.         if (i >= 255) {
  155.             pStr[0] = (unsigned char)255;
  156.             return(FALSE);
  157.         }
  158.     }
  159.     pStr[0] = (unsigned char)(i - 1);
  160.     return(TRUE);
  161. }
  162.  
  163. Boolean XTptocstr(pStr,cStr)
  164. char    *pStr,*cStr;
  165. {
  166.     int     i;
  167.  
  168.     if (!ValidPointer(cStr) || !ValidPointer(pStr))
  169.         return(FALSE);
  170.  
  171.     for (i = 0;i < (unsigned char)pStr[0];i++) {
  172.         cStr[i] = cStr[i+1];
  173.     }
  174.     cStr[i+1] = '\0';
  175.     return(TRUE);
  176. }
  177.  
  178. Boolean XTpstrconcat(toStr,fromStr)
  179. char    *toStr,*fromStr;
  180. {
  181.     int     oldLen,newLen,i;
  182.  
  183.     if ((toStr == NULL) || (fromStr == NULL))
  184.         return(FALSE);
  185.  
  186.     oldLen = (unsigned char)(toStr[0]);
  187.     newLen = (unsigned char)(fromStr[0]);
  188.     if (oldLen + newLen > 255)
  189.         return(FALSE);
  190.  
  191.     toStr[0] = oldLen + newLen;
  192.     for (i=1; i <= newLen; i++)
  193.         toStr[oldLen+i] = fromStr[i];
  194.  
  195.     return(TRUE);
  196. }
  197.